package com.jdls.tanhua.dubbo.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jdls.tanhua.dto.RecommendUserDto;
import com.jdls.tanhua.dubbo.UserInfoApi;
import com.jdls.tanhua.dubbo.mapper.UserInfoMapper;
import com.jdls.tanhua.pojo.BlackList;
import com.jdls.tanhua.pojo.UserInfo;
import com.jdls.tanhua.vo.PageResult;
import com.jdls.tanhua.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 嘉德罗斯
 * @date 2021/10/23
 */
@DubboService
public class UserInfoApiImpl implements UserInfoApi {


    @Autowired
    private UserInfoMapper mapper;

    @Override
    public void save(UserInfo userInfo) {
        mapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        String birthday = userInfo.getBirthday();
        long date = System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(birthday);
        } catch (ParseException e) {
        }
        long time = parse.getTime();
        Long a = date-time;
        long l = a / (365 * 24 * 60 * 60 * 1000L);
        userInfo.setAge(String.valueOf(l));
        mapper.updateById(userInfo);
    }

    @Override
    public UserInfo find(Long id) {
        UserInfo userInfo = mapper.selectById(id);
        return userInfo;
    }

    @Override
    public List<UserInfo> findAll(List<Long> ids) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserInfo::getId,ids);
        List<UserInfo> list = mapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<UserInfo> findAll(List<Long> ids, String keyword) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserInfo::getId,ids);
        queryWrapper.like(keyword!=null,UserInfo::getNickname,keyword);
        List<UserInfo> list = mapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<UserInfo> findScreen(List<Long> ids, RecommendUserDto recommendUserDto) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserInfo::getId,ids);
        //queryWrapper.lt(recommendUserDto.getAge()!=null,UserInfo::getAge,recommendUserDto.getAge());
        //queryWrapper.eq(recommendUserDto.getCity()!=null,UserInfo::getCity,recommendUserDto.getCity());
        //queryWrapper.eq(recommendUserDto.getGender()!=null,UserInfo::getGender,recommendUserDto.getGender());
        //queryWrapper.eq(recommendUserDto.getEducation()!=null,UserInfo::getEducation,recommendUserDto.getEducation());
        List<UserInfo> list = mapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<UserInfo> findUserInfos(List<Long> friendIds) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserInfo::getId,friendIds);
        List<UserInfo> list = mapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<UserInfo> findSex(List<Long> list, String gender) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserInfo::getId,list);
        queryWrapper.eq(gender!=null,UserInfo::getGender,gender);
        List<UserInfo> userInfos = mapper.selectList(queryWrapper);
        return userInfos;
    }


}
